/**
@author		Jong Min, Lee
@date		  01/22/2014
@breif		Monitor window class

copyright(c) Tae Young Soft. All right reserved. 2014
*/
#include "stdafx.h"
#include "Taurus_Viewer/MonitorWnd.h"

IMPLEMENT_DYNAMIC(CMonitorWnd, CFrameWnd)

CMonitorWnd::CMonitorWnd()
{
  init();
}

CMonitorWnd::~CMonitorWnd()
{
  destroy();
}

BOOL CMonitorWnd::create(const CRect& rcRect, 
                         CWnd* pParentWnd, 
                         CreateParams& stCreateParams)
{
  CString strClassName = AfxRegisterWndClass(CS_HREDRAW | CS_VREDRAW,
    ::LoadCursor(NULL, IDC_ARROW),
    (HBRUSH)::GetStockObject(BLACK_BRUSH),
    ::LoadIcon(NULL, IDI_APPLICATION));

  m_strTitle = _T("");

  return CreateEx(WS_EX_ACCEPTFILES,
                  strClassName,
                  _T(""),
                  WS_POPUP,
                  rcRect,
                  pParentWnd,
                  0,
                  reinterpret_cast<LPVOID>(&stCreateParams));
}

void CMonitorWnd::init()
{
  m_theMonitor = NULL;
  m_theMonitorIndex = 0;
}

void CMonitorWnd::destroy()
{
}

BOOL CMonitorWnd::validateCreateParams(const CreateParams* pParams) const
{
  if(pParams == NULL)
    return FALSE;

  if(pParams->theParentMonitor == NULL)
    return FALSE;

  return TRUE;
}

BEGIN_MESSAGE_MAP(CMonitorWnd, CWnd)
  ON_WM_CREATE()
  ON_WM_PAINT()
  ON_WM_ERASEBKGND()
  ON_WM_NCPAINT()
  ON_WM_NCCALCSIZE()
END_MESSAGE_MAP()

BOOL CMonitorWnd::PreCreateWindow(CREATESTRUCT& cs)
{
  cs.style &= ~(WS_BORDER | WS_DLGFRAME | WS_THICKFRAME | WS_CLIPCHILDREN);
  if(!CFrameWnd::PreCreateWindow(cs))
    return FALSE;

  if(cs.hMenu != NULL)
  {
    ::DestroyMenu(cs.hMenu);
    cs.hMenu = NULL;
  }
  return TRUE;
}

int CMonitorWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
  LPVOID lpCreateParams = lpCreateStruct->lpCreateParams;
  lpCreateStruct->lpCreateParams = NULL;

  int nRet = CFrameWnd::OnCreate(lpCreateStruct);

  lpCreateStruct->lpCreateParams = lpCreateParams;

  if(nRet == -1)
    return -1;

  SetMenu(0);
  ModifyStyleEx(WS_BORDER, 0);

  CMonitorWnd::CreateParams* pstCreateParams =
    reinterpret_cast<CMonitorWnd::CreateParams*>(lpCreateStruct->lpCreateParams);

  if(!validateCreateParams(pstCreateParams))
    return -1;

  m_theMonitor = pstCreateParams->theParentMonitor;
  m_theMonitorIndex = pstCreateParams->theMonitorIndex;
  return 0;
}

void CMonitorWnd::OnPaint()
{
  CPaintDC dc(this);

  CRect rcClient;
  GetClientRect(&rcClient);

  dc.FillSolidRect(rcClient, RGB(45, 45, 48));
}

BOOL CMonitorWnd::OnEraseBkgnd(CDC* pDC)
{
  UNUSED_ALWAYS(pDC);

  return TRUE;
}

void CMonitorWnd::OnNcPaint()
{
  CRect rcWindow;
  GetWindowRect(rcWindow);
  CRect rcClient;
  GetClientRect(rcClient);
  ClientToScreen(&rcClient);

  rcClient.OffsetRect(-rcWindow.TopLeft());
  rcWindow.OffsetRect(-rcWindow.TopLeft());

  CWindowDC dcWindow(this);
  CRect rcTop(rcWindow.left, rcWindow.top, rcWindow.right, rcClient.top);
  dcWindow.FillSolidRect(rcTop, RGB(45, 45, 48));

  CRect rcBottom(rcWindow.left, rcClient.bottom, rcWindow.right, rcWindow.bottom);
  dcWindow.FillSolidRect(rcBottom, RGB(45, 45, 48));

  CRect rcLeft(rcWindow.left, rcWindow.top, rcClient.left, rcWindow.bottom);
  dcWindow.FillSolidRect(rcLeft, RGB(45, 45, 48));

  CRect rcRight(rcClient.right, rcWindow.top, rcWindow.right, rcWindow.bottom);
  dcWindow.FillSolidRect(rcRight, RGB(45, 45, 48));
}

void CMonitorWnd::OnNcCalcSize(BOOL bCalcValidRects, NCCALCSIZE_PARAMS* lpncsp)
{
  lpncsp->rgrc[0].left += 0;
  lpncsp->rgrc[0].top += 0;
  lpncsp->rgrc[0].right -= 0;
  lpncsp->rgrc[0].bottom -= 0;

  CFrameWnd::OnNcCalcSize(bCalcValidRects, lpncsp);
}

